<? include 'server_config.php'; 

if($_SESSION['user_idx'] == "")
{
	?>
	<script>
		location.href='login.php';
	</script>
	<?
	return;
}

?>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<?


if ($document_idx=="")
{
	$sql = "
	INSERT INTO ndd_document
	VALUES
	(
	'', '$board_idx', '$user_idx', '$category_idx', '$title', '$content',
	0, 0, 0, 0, '".$_SESSION['user_idx']."', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
	";

	//echo $sql;
	$result = mysql_query($sql,$connect);


	$sql = "
	SELECT MAX(document_idx) FROM ndd_document
	WHERE board_idx = '$board_idx'
	AND user_idx = '$user_idx'
	AND category_idx = '$category_idx'
	";
	$result = mysql_query($sql,$connect);
	$row = mysql_fetch_row($result);

	$document_idx = $row[0];

	if($board_idx==2)
	{
		//팀게시판이므로 새글 등록 알림

		//http://sajaa.cafe24.com/ndd/board_document.php?name=team&category_idx=0&page=1&document_idx=6&action=view


		$name = "넥도닥";
		$frommail = "no-replay@nextdoordoctor.co.kr";
		$tomail = "<hotae913@gmail.com>, <amh1987@naver.com>, <sunshine.somin@gmail.com>, <seunghwonyi@gmail.com>";
		$subject = "[넥도닥] 게시판에 새 글이 등록되었습니다.";
		$memo = "게시판에 새 글이 등록되었습니다.<br><br>제목 : ".$title."<br><br><a href='http://sajaa.cafe24.com/ndd/board_document.php?name=team&category_idx=".$category_idx."&document_idx=".$document_idx."&action=view'><font color='blue'>글 보러가기</font></a> 링크를 클릭하면 해당 글 페이지로 이동합니다.<br><br>";


		/* mail()함수에서 쓰게될 변수 */

		$mh.="Return-Path: $frommail\r\n";
		$mh.="From: $name <$frommail>\r\n";
		$mh.= "Content-Type: text/html; charset=\"UTF-8\"\r\n";
		/* 줄의 끝에 붙어 있는 슬래쉬를 제거하는 함수 */

	//	$memo=stripslashes($memo);


		/* 메일을 보내주는 함수 */
/*
		echo $tomail."<br>";
		echo $subject."<br>";
		echo $memo."<br>";
		echo $mh."<br>";
*/
		//mail($tomail, $subject, $memo, $mh);

	}

}
else
{

	$sql = "
	SELECT user_idx FROM ndd_document
	WHERE document_idx = '$document_idx'
	";
	$result = mysql_query($sql,$connect);
	$row = mysql_fetch_row($result);

	$document_user_idx = $row[0];

	if ($_SESSION['user_idx']==$document_user_idx)
	{
		$sql = "UPDATE ndd_document SET title = '$title', content='$content', modify_timestamp = CURRENT_TIMESTAMP
		WHERE document_idx= '$document_idx' ;  ";

		$result = mysql_query($sql,$connect);

	}

}



	$path = "./upload/";

	for($i=1;$i<=5;$i++)
	{
		$att = "attachfile_".$i;

		if ($_FILES[$att]["error"] > 0 || $_FILES[$att]["size"]==0)
		  {
//		  echo "Error: " . $_FILES[$att]["error"] . "<br />";
		  }
		else
		  {
		  echo "Upload: " . $_FILES[$att]["name"] . "<br />";
		  echo "Type: " . $_FILES[$att]["type"] . "<br />";
		  echo "Size: " . ($_FILES[$att]["size"] / 1024) . " Kb<br />";
		  echo "Stored in: " . $_FILES[$att]["tmp_name"]."<br>";

		
			$sql2="SELECT MAX(file_idx) FROM ndd_file;";
			$result2 = mysql_query($sql2,$connect);
			$row2 = mysql_fetch_row($result2);
			$new_file_idx = $row2[0]+1;

			$uploaded_name = "b_".$document_idx."_".$new_file_idx;

		  echo move_uploaded_file($_FILES[$att]["tmp_name"],
		  $path.$uploaded_name);

			$sql2="
				INSERT INTO ndd_file 
				VALUES ('', '$document_idx', '$uploaded_name', '".$_FILES[$att]['name']."', 0, ".($_FILES[$att]["size"] / 1024).");";
			$result2 = mysql_query($sql2,$connect);

		  }

	}





// 태그등록

$sql = "
DELETE FROM ndd_document_tag WHERE document_idx = '$document_idx';
";
$result = mysql_query($sql,$connect);


$tag_list = explode(",",$tags);

foreach ($tag_list as $tag)
{
	$tag = str_replace(" ","",$tag);

	if($tag=="")
		continue;



	$sql = "
	INSERT INTO ndd_list_tag (word) VALUES ( '$tag' );
	";
	$result = mysql_query($sql,$connect);

	$sql = "
	SELECT tag_idx FROM ndd_list_tag WHERE word = '$tag' LIMIT 1;
	";
	$result = mysql_query($sql,$connect);
	$row = mysql_fetch_row($result);

	$tag_idx = $row[0];

	$sql = "
	INSERT INTO ndd_document_tag (document_idx, user_idx, tag_idx) VALUES ( $document_idx, $user_idx, $tag_idx );
	";

//	echo $sql."<br>";
	$result = mysql_query($sql,$connect);

}


?>

<script> 
//location.replace("board_document.php?name=<? echo $board_name;?>&category=<? echo $category_idx; ?>&document_idx=<? echo $document_idx; ?>&page=<? echo $page; ?>&action=view");
location.replace("ndd_talk.php?name=<? echo $board_name;?>&category=<? echo $category_idx; ?>&document_idx=<? echo $document_idx; ?>&page=<? echo $page; ?>&action=view");

</script>











